//
//  WBHomeViewController.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import YYModel
private let identifier = "homeCell"

class WBHomeViewController: WBBaseViewController {
    //HomeViewController的viewModel
    lazy var homeListViewModel: WBHomeListViewModel = WBHomeListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 如果是登录，修改导航条的tile
        if WBUserAccount.shared.isLogin {
            self.title = WBUserAccount.shared.screen_name!
            self.tabBarItem.title = "首页"
        }
        
        // 加载数据
        if WBUserAccount.shared.isLogin {
            loadData()
        }
        
        // 接收点击配图视图imageView的通知
        NotificationCenter.default.addObserver(self, selector: #selector(statusImageClicked(notification:)), name: Notification.Name(rawValue: statusImageClikedNotification), object: nil)
    }

}

extension WBHomeViewController {
    func statusImageClicked(notification: Notification) {
        if let userInfo = notification.userInfo as? [String: Any] {
            let index = userInfo["index"] as! Int
            let urls = userInfo["urls"] as! [String]
            present(WBPhotoBroserViewController(index: index, urls: urls), animated: false, completion: nil)
        }
    }
}

// MARK: - setupTableView/获取数据
extension WBHomeViewController {
    
    /// setupTableView
    override func setupTableView() {
        super.setupTableView()

        // 注册原型 cell
        tableView?.register(WBHomeCell.self, forCellReuseIdentifier: identifier)
    }
    
    /// 获取数据
    override func loadData() {
        // 不super则可以控制停止刷新
        
        //用refreshHeader判断是上拉,还是下拉
        let isPullDown = refreshHeader.isRefreshing()
        //调用viewModel来获取数据, 完成后刷新表格
        homeListViewModel.loadData(isPullDown: isPullDown) {
            (success) in
            
            //更新UI 停止刷新控件刷新
            if isPullDown {
                self.refreshHeader.endRefreshing()
            }else {
                self.refreshFooter.endRefreshing()
            }
            
            //获取数据成功后,刷新表格
            if success {
                self.tableView?.reloadData()
            }
            
        }
    }
    
}

// MARK: - tableView数据源
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //向viewModel要数据
        return homeListViewModel.statusViewModesArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! WBHomeCell
        cell.selectionStyle = .none
        
        //取到statusModel, 赋值给cell
        let statusViewModel = homeListViewModel.statusViewModesArray[indexPath.row]
        cell.homeViewModel = statusViewModel
        
        return cell
    }
    
}

// MARK: - tableViewDelegate
extension WBHomeViewController {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let statusViewModel = homeListViewModel.statusViewModesArray[indexPath.row]
//        print("============rowheight:\(homeListViewModel.statusViewModesArray[indexPath.row].cellHeight)")
        return statusViewModel.cellHeight + statusLayout.margin/2
        
    }
}
